package com.comp6231.frontend;

import com.comp6231.common.*;

import java.util.concurrent.atomic.AtomicInteger;

import DPIS.StationServerPOA;

/**
 * 
 * @author sknarula (narula.sk@gmail.com)
 * @since November 2013
 * 
 *        This is the class which implements the ServerStationInterface. It
 *        provides the functionalities to be performed by the server.
 * 
 */
public class StationServerImplementation extends StationServerPOA {

	/**
	 * Attribute to use to assign request id to the incoming requests
	 */
	protected volatile AtomicInteger requestId = new AtomicInteger(0);

	@Override
	public boolean createCRecord(String badgeId, String firstName,
			String lastName, String crimeDescription, String criminalStatus) {

		int requestNum = requestId.getAndIncrement();
		// create the msg string
		String arguements = badgeId + " ## " + firstName + " ## " + lastName
				+ " ## " + crimeDescription + " ## " + criminalStatus;
		String message = (new Request(FrontEndInitiator.frontEnd_hostName,
				new Integer(FrontEndInitiator.frontEnd_UDPPort_data),
				"FrontEnd", String.valueOf(requestNum), "createCRecord",
				arguements)).createPacketString();

		// add the request to the request map table kept at front end
		FrontEndInitiator.clientRequests.put(requestNum, message);

		// process the request
		String response = this.processRequest(message, requestNum);
		
		return (response.toLowerCase().startsWith("success") ? true : false);
	}

	@Override
	public boolean createMRecord(String badgeId, String firstName,
			String lastName, String lastKnownAddress, String lastSeenDate,
			String lastSeenPlace, String missingStatus) {

		int requestNum = requestId.getAndIncrement();
		// create the msg string
		String arguements = badgeId + " ## " + firstName + " ## " + lastName
				+ " ## " + lastKnownAddress + " ## " + lastSeenDate + " ## "
				+ lastSeenPlace + " ## " + missingStatus;
		String message = (new Request(FrontEndInitiator.frontEnd_hostName,
				new Integer(FrontEndInitiator.frontEnd_UDPPort_data),
				"FrontEnd", String.valueOf(requestNum), "createMRecord",
				arguements)).createPacketString();

		// add the request to the request map table kept at front end
		FrontEndInitiator.clientRequests.put(requestNum, message);

		// process the request
		String response = this.processRequest(message, requestNum);
				
		return (response.toLowerCase().startsWith("success") ? true : false);
	}

	@Override
	public String getRecordCounts(String badgeId) {

		int requestNum = requestId.getAndIncrement();
		// create the msg string
		String arguements = badgeId;
		String message = (new Request(FrontEndInitiator.frontEnd_hostName,
				new Integer(FrontEndInitiator.frontEnd_UDPPort_data),
				"FrontEnd", String.valueOf(requestNum), "getRecordCounts",
				arguements)).createPacketString();

		// add the request to the request map table kept at front end
		FrontEndInitiator.clientRequests.put(requestNum, message);

		// process the request
		String response = this.processRequest(message, requestNum);
								
		return response;
	}

	@Override
	public boolean editRecord(String badgeId, String lastName, String recordId,
			String newStatus) {

		int requestNum = requestId.getAndIncrement();
		// create the msg string
		String arguements = badgeId + " ## " + lastName + " ## " + recordId
				+ " ## " + newStatus;
		String message = (new Request(FrontEndInitiator.frontEnd_hostName,
				new Integer(FrontEndInitiator.frontEnd_UDPPort_data),
				"FrontEnd", String.valueOf(requestNum), "editRecord",
				arguements)).createPacketString();

		// add the request to the request map table kept at front end
		FrontEndInitiator.clientRequests.put(requestNum, message);

		// process the request
		String response = this.processRequest(message, requestNum);
				
		return (response.toLowerCase().startsWith("success") ? true : false);
	}

	@Override
	public boolean transferRecord(String badgeId, String recordID,
			String remoteStationServerName) {

		int requestNum = requestId.getAndIncrement();
		// create the msg string
		String arguements = badgeId + " ## " + recordID + " ## "
				+ remoteStationServerName;
		String message = (new Request(FrontEndInitiator.frontEnd_hostName,
				new Integer(FrontEndInitiator.frontEnd_UDPPort_data),
				"FrontEnd", String.valueOf(requestNum), "transferRecord",
				arguements)).createPacketString();

		// add the request to the request map table kept at front end
		FrontEndInitiator.clientRequests.put(requestNum, message);

		// process the request
		String response = this.processRequest(message, requestNum);
		
		return (response.toLowerCase().startsWith("success") ? true : false);
	}

	/**
	 * This method starts the new thread and wait for it to finish
	 * 
	 * @param message
	 */
	private String processRequest(String message, int requestNum) {

		// send the request to coordinator
		ProcessClientRequest processClientRequest = new ProcessClientRequest(message,
				requestNum);
		processClientRequest.process();
		
		// reaching here means that the request is processed and the string
		// message in hashtable clientRequests for this
		// request id is replaced with the actual result 		
		return FrontEndInitiator.clientRequests.get(requestNum).trim();
	}

	@Override
	public boolean transfer(String[] record) {
		// Only for use in Replica One
		return false;
	}

	@Override
	public void printHashTable() {
		// Only for use in Replica One
	}

}
